package cn.ssm.petadopt.service.impl;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import cn.ssm.petadopt.codeMsg.CodeMsg;
import cn.ssm.petadopt.dto.AdminsLoginDTO;
import cn.ssm.petadopt.dto.PwdSettingDTO;
import cn.ssm.petadopt.dto.SelectPageDTO;
import cn.ssm.petadopt.dto.UserLoginDTO;
import cn.ssm.petadopt.entity.Admins;
import cn.ssm.petadopt.entity.User;
import cn.ssm.petadopt.mapper.AdminsMapper;
import cn.ssm.petadopt.service.AdminsService;
import cn.ssm.petadopt.util.ResponseResult;
import com.alibaba.druid.support.json.JSONUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @Autheor: Sun俊仪
 * @Date: 2022 2022/12/17 14:33
 * @Name: PetSystem
 */
@Service
@Transactional
public class AdminsServiceImpl implements AdminsService {

    @Autowired
    private AdminsMapper adminsMapper;

    @Override
    public ResponseResult loginA(AdminsLoginDTO adminsLoginDTO, HttpSession session) {
        String adminName = adminsLoginDTO.getAdminName();
        String adminPwd = adminsLoginDTO.getAdminPwd();
        String captcha = adminsLoginDTO.getCaptcha();
        ResponseResult responseResult = ResponseResult.builder().build();
        // 判断验证码
        String code = (String) session.getAttribute("code");
        if (captcha.equals(code)){
            // 通过userName从数据库中获取该user对象的信息
            Admins admin = adminsMapper.selectAdminByAdminName(adminName);
            if (admin!=null){
                if (admin.getAdminPwd().equals(adminPwd)){
                    // 登录成功  将该用户信息存入到session域对象中
                    session.setAttribute("admin",admin);
                    // 返回成功登录信息      前端根据登录成功的code进行跳转
                    responseResult.setCode(CodeMsg.LOGIN_SUCCESS.getCode());
                    responseResult.setMsg(CodeMsg.LOGIN_SUCCESS.getMsg());

                }else {
                    // 密码错误
                    responseResult.setCode(CodeMsg.PASSWORD_FAILED.getCode());
                    responseResult.setMsg(CodeMsg.PASSWORD_FAILED.getMsg());
                }
            }else{
                // 用户名不存在
                responseResult.setCode(CodeMsg.USERNAME_FAILED.getCode());
                responseResult.setMsg(CodeMsg.USERNAME_FAILED.getMsg());
            }
        }else{
            //  返回验证码错误的msg
            responseResult.setCode(CodeMsg.CODE_FAILED.getCode());
            responseResult.setMsg(CodeMsg.CODE_FAILED.getMsg());
        }
        return responseResult;
    }

    @Override
    public ResponseResult selectPage(SelectPageDTO selectPageDTO) {
        System.out.println("selectPageDTO = " + selectPageDTO);
        Integer page = selectPageDTO.getPage();
        Integer limit = selectPageDTO.getLimit();
        String searchParams = selectPageDTO.getSearchParams();
        Admins admins = JSONUtil.toBean(searchParams, Admins.class);

        ResponseResult responseResult = ResponseResult.builder().build();

        // 分页查询————mybatis提供的插件
        PageHelper.startPage(page,limit);
        // 查询所有数据
        List<Admins> adminsList = adminsMapper.selectPage(admins);

        PageInfo<Admins> adminsListPageInfo = new PageInfo<>(adminsList);
        long total = adminsListPageInfo.getTotal();

        // 判断结果
        responseResult.setCode(CodeMsg.SELECTPAGE_SUCCESS.getCode());
        responseResult.setMsg(CodeMsg.SELECTPAGE_SUCCESS.getMsg());
        responseResult.setData(adminsList);
        responseResult.setCount(total);

        return responseResult;
    }

    @Override
    public ResponseResult updateA(Admins admin,HttpSession session) {
        ResponseResult responseResult = ResponseResult.builder().build();
        Integer line = adminsMapper.updateA(admin);
        if (line!=null){
            responseResult.setCode(CodeMsg.UPDATEA_SUCCESS.getCode());
            responseResult.setMsg(CodeMsg.UPDATEA_SUCCESS.getMsg());
            // 重新赋值session域对象中的admin信息
            Admins admin1 = adminsMapper.selectAdminByAdminName(admin.getAdminName());
            session.setAttribute("admin",admin1);
            return responseResult;
        }

        responseResult.setCode(CodeMsg.UPDATEA_FAILED.getCode());
        responseResult.setMsg(CodeMsg.UPDATEA_FAILED.getMsg());
        return responseResult;

    }

    @Override
    public ResponseResult updatePwd(PwdSettingDTO pwdSettingDTO, HttpSession session) {
        String adminPwd = pwdSettingDTO.getAdminPwd();
        String newPassword = pwdSettingDTO.getNewPassword();
        String againPassword = pwdSettingDTO.getAgainPassword();
        ResponseResult responseResult = ResponseResult.builder().build();

        Admins admin = (Admins) session.getAttribute("admin");
        String adminName = admin.getAdminName();
        if (adminPwd.equals(admin.getAdminPwd())){
            // 判断两次密码是相同
            if (newPassword.equals(againPassword)){
                admin.setAdminPwd(newPassword);
                session.setAttribute("admin",admin);
                // 根据adminName更新数据
                Integer line = adminsMapper.updatePwd(admin);
                if (line!=null){
                    responseResult.setCode(200);
                    responseResult.setMsg("密码修改成功");
                    return  responseResult;
                }
            }else {
                responseResult.setCode(CodeMsg.JUDGEPWD_FAILED.getCode());
                responseResult.setMsg(CodeMsg.JUDGEPWD_FAILED.getMsg());
            }
        }else {
            responseResult.setCode(CodeMsg.OLDPWDSETTING_FAILED.getCode());
            responseResult.setMsg(CodeMsg.OLDPWDSETTING_FAILED.getMsg());
        }
        return responseResult;
    }

    @Override
    public Integer uploadPic(Admins admin) {
        Integer line = adminsMapper.uploadPic(admin);
        return line;
    }

    @Override
    public ResponseResult addJudgeAdminName(String adminName) {
        ResponseResult responseResult = ResponseResult.builder().build();
        Admins admin = adminsMapper.selectAdminByAdminName(adminName);
        if (admin==null){
            responseResult.setCode(200);
            responseResult.setMsg("用户名可用");
        }else {
            responseResult.setCode(500);
            responseResult.setMsg("用户名已被注册");
        }
        return  responseResult;
    }

    @Override
    public ResponseResult addOneAdmin(Admins admin) {
        ResponseResult responseResult = ResponseResult.builder().build();
        // 默认密码123
        admin.setAdminPwd("123");
        Integer line = adminsMapper.addOneAdmin(admin);
        if (line!=null){
            responseResult.setCode(200);
            responseResult.setMsg("管理员添加成功");
        }else {
            responseResult.setCode(500);
            responseResult.setMsg("管理员添加失败");
        }
        return responseResult;
    }

    @Override
    public ResponseResult selectAdminById(Integer id) {
        ResponseResult responseResult = ResponseResult.builder().build();
        Admins admin =  adminsMapper.selectAdminById(id);
        if (admin!=null){
            responseResult.setData(admin);
            responseResult.setCode(200);
            responseResult.setMsg("用户数据获取成功");
        }else{
            responseResult.setCode(500);
            responseResult.setMsg("用户数据获取失败");
        }
        return responseResult;
    }
}

